<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <button onclick="a()">press me</button>
</body>
</html>
<script>
    let socket1 = null;
    let url = "127.0.0.1:8080"
    if(typeof (WebSocket) == "undefined"){
        console.log("您的浏览器不支持websocket");
    }else{
        let socketUrl = "ws://"+url+"/websocket/test";
        // if(socket1!=null){
        //     socket1.close();
        //     socket1 = null;
        // }
        socket1 = new WebSocket(socketUrl);
        //打开事件
        socket1.onopen = function () {
            console.log("websocket1已经打开");
        }
        //获取消息事件.
        socket1.onmessage = function (msg) {
            let data = JSON.parse(msg.data);
            if(data.type===0)//用户登录提醒
            {
                console.log("用户名重复！");
            }else if(data.type===1)
            {
                that.lobby_number = data.onlineNum;
            }else if(data.type===2){//收到消息推送
                //先判断是不是自己发的
                if(data.name!=that.name_id){
                    let message = [];
                    message.type = 'in';
                    message.content = data.content;
                    if(data.name.substr(0,2)==="游客")
                    {
                        message.name = data.name.substr(2,data.name.lastIndexOf("(")-2)+"(游客)";
                    }else{
                        message.name = data.name.substr(data.name.lastIndexOf("|")+1,data.name.length-data.name.lastIndexOf("|")+1);
                    }
                    message.url = data.url;
                    message.time = data.time;
                    that.lobby_messages.push(message);
                    that.scroll();//话到底
                }
            }
        }
    }

    a=function () {
        let message = {msg:"nihao"};
        socket1.send(JSON.stringify(message));
        console.log("发生成功！");
    }


</script>